#include "sdk_common.h"
#include "cbstyledtextctrl.h"
#include "cbeditorprintout.h"
#include <wx/paper.h>

cbEditorPrintout::cbEditorPrintout(const wxString& title, cbStyledTextCtrl* control, bool selectionOnly)
  : wxPrintout(title),
    m_TextControl(control) {
  m_SelStart = 0;
  m_SelEnd = control->GetLength();
  if(selectionOnly && !control->GetSelectedText().IsEmpty()) {
    m_SelStart = control->GetSelectionStart();
    m_SelEnd = control->GetSelectionEnd();
  }
  m_pPageSelStart = new wxArrayInt;
}

cbEditorPrintout::~cbEditorPrintout() {
  delete m_pPageSelStart;
  m_pPageSelStart = nullptr;
}

bool cbEditorPrintout::OnPrintPage(int page) {
  wxDC *dc = GetDC();
  if(dc) {
    ScaleDC(dc);
    int maxpage = m_pPageSelStart->GetCount();
    if(page && page < maxpage)
      m_printed = (*m_pPageSelStart)[page - 1];
    else {
      Manager::Get()->GetLogManager()->DebugLog(F(_T("OnPrintPage ERROR: page = %d , maxpage = %d"), page, maxpage));
      return false;
    }
    m_printed = m_TextControl->FormatRange(1, m_printed, m_SelEnd,
                                           dc, dc, m_printRect, m_pageRect);
    return true;
  }
  return false;
}

bool cbEditorPrintout::HasPage(cb_unused int page) {
  return (m_printed < m_SelEnd);
}

void cbEditorPrintout::GetPageInfo(int *minPage, int *maxPage, int *selPageFrom, int *selPageTo) {
//  *minPage = 0;
//  *maxPage = 0;
//  *selPageFrom = 0;
//  *selPageTo = 0;
//  wxSize ppiScr;
//  GetPPIScreen(&ppiScr.x, &ppiScr.y);
//  if(ppiScr.x == 0 || ppiScr.y == 0) {
//    ppiScr.x = 96;
//    ppiScr.y = 96;
//  }
//
//  wxDC *dc = GetDC();
//  if(dc) {
//    ScaleDC(dc);
//    m_pPageSelStart->Clear();
//    m_pPageSelStart->Add(m_SelStart);
//    m_printed = m_SelStart;
//    while(HasPage(*maxPage)) {
//      m_printed = m_TextControl->FormatRange(0, m_printed, m_SelEnd, dc, dc, m_printRect, m_pageRect);
//      m_pPageSelStart->Add(m_printed);
//      *maxPage += 1;
//    }
//  } else
//    *maxPage = 32000;
//  if(*maxPage > 0)
//    *minPage = 1;
//  *selPageFrom = *minPage;
//  *selPageTo = *maxPage;
//  m_printed = m_SelStart;
}

bool cbEditorPrintout::OnBeginDocument(int startPage, int endPage) {
  bool result = wxPrintout::OnBeginDocument(startPage, endPage);
  int maxpage = m_pPageSelStart->GetCount();
  if(startPage > maxpage || endPage > maxpage) {
    Manager::Get()->GetLogManager()->DebugLog(F(_T("OnBeginDocument ERROR: startPage %d , endPage %d , maxpage %d "), startPage, endPage, maxpage));
    return false;
  }
  return result;
}

bool cbEditorPrintout::ScaleDC(wxDC *dc) {
  if(!dc)
    return false;
  wxSize ppiScr;
  GetPPIScreen(&ppiScr.x, &ppiScr.y);
  if(ppiScr.x == 0) {
    ppiScr.x = 96;
    ppiScr.y = 96;
  }
  wxSize ppiPrt;
  GetPPIPrinter(&ppiPrt.x, &ppiPrt.y);
  if(ppiPrt.x == 0) {
    ppiPrt.x = ppiScr.x;
    ppiPrt.y = ppiScr.y;
  }
  wxSize dcSize = dc->GetSize();
  wxSize pageSize;
  GetPageSizePixels(&pageSize.x, &pageSize.y);
  float scale_x = (float)(ppiPrt.x * dcSize.x) /
                  (float)(ppiScr.x * pageSize.x);
  float scale_y = (float)(ppiPrt.y * dcSize.y) /
                  (float)(ppiScr.y * pageSize.y);
  dc->SetUserScale(scale_x, scale_y);
  return true;
}
